package com.rtline.business.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.rtline.business.service.UserOperationStrategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>UserOperationStrategyFactory .
 *
 * <p>change history:
 *
 * <pre>
 * date         defect       person          comments
 * --------------------------------------------------------------------------------
 * 2025/5/31    ********     Tao Ren    create file.
 * </pre>
 *
 * @author Tao Ren
 * @date 2025/5/31 13:45
 */
@Service
public class UserOperationStrategyFactory {
    private final Map<String, UserOperationStrategy> strategies;

    @Autowired
    public UserOperationStrategyFactory(List<UserOperationStrategy> strategies) {
        //构造方法注入
        this.strategies = strategies.stream()
                .collect(Collectors.toMap(this::getStrategyName, strategy -> strategy));
    }

    private String getStrategyName(UserOperationStrategy strategy) {
        String className = strategy.getClass().getSimpleName();
        return className.substring(0, className.lastIndexOf("StrategyImpl")).toLowerCase();
    }

    public UserOperationStrategy getStrategy(String operationType) {
        return strategies.get(operationType);
    }
}
